
// npnjs.com  to search passport-jwt and use code
const JwtStrategy = require('passport-jwt').Strategy,
ExtractJwt = require('passport-jwt').ExtractJwt;

// guide from User.js mongoose.model('Users',userSchema)
const User = require('mongoose').model("Users")
const keys = require('./db')


var opts = {}
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = keys.tokenKey;
module.exports = passport => {
    passport.use(new JwtStrategy(opts, function(jwt_payload, done) {
        // console.log(jwt_payload)
        User.findById(jwt_payload.id)
            .then(user => {
                if (user) {
                    return done(null,user)
                }
                return done(null,false)
            })
            .catch(err => console.log(err))  
    }));
}